//Lou Decroo
//Modified 12-04-2009

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class QuestionPanel extends JPanel implements ActionListener, FocusListener{
	private JPanel main, left, right, rowq, row1, row2, row3, row4, row5;
	private String[] possibleAns;
	private JLabel	quesNumLabel, quesLabel;
	private JTextArea oeResponse;
	private JRadioButton[] radioButtons;
	private JButton removeButton;
	private ButtonGroup buttonGroup;
	private JScrollPane scroll;
	private Response usersResponse;
	private int type;
	private int qid;
		
	public QuestionPanel(Question q, int quesNum, int uStat){
		// Create main layout and flow layouts for each row
		this.setLayout(new BorderLayout());
		main = new JPanel(new GridLayout(6,1));
		left = new JPanel(new FlowLayout());
		right = new JPanel(new FlowLayout());
		rowq = new JPanel(new FlowLayout());
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4 = new JPanel(new FlowLayout());
		row5 = new JPanel(new FlowLayout());
		
		// set possible answers question number label and question label
		possibleAns = q.getPossibleAns();
		quesNumLabel = new JLabel(quesNum+".");
		quesLabel = new JLabel(q.getContent());
		type = q.getType();
		qid = q.getQid();
		removeButton = new JButton("REMOVE");
		removeButton.addActionListener(this);
		
		// add a question number and question label
		left.add(quesNumLabel);
		rowq.add(quesLabel);
		System.out.println("default? " + q.isDefault() + " uStat: " + uStat);
		if(!q.isDefault() && uStat == 2)
		{
			System.out.println("Add a remove button");
			right.add(removeButton);
		}
		main.add(rowq);

		// checks if question is of multiple choice type
		if(q.getType() == 2){
			System.out.println("multiple choice " + q.getContent());
			// create radio buttons for each possible answer
			radioButtons = new JRadioButton[q.getPossibleAns().length];
			System.out.println(radioButtons.length);
			for(int i=0; i<q.getPossibleAns().length; i++){
				radioButtons[i] = new JRadioButton(possibleAns[i]);
				radioButtons[i].addActionListener(new RadioButtonHandler());
			}
			
			// create a button group and group all radio buttons and responses
			buttonGroup = new ButtonGroup();
			
			if(q.getPossibleAns().length >0){
				buttonGroup.add(radioButtons[0]);
				row1.add(radioButtons[0]);
				main.add(row1);
			}
			
			if(q.getPossibleAns().length >1){
				buttonGroup.add(radioButtons[1]);
				row2.add(radioButtons[1]);
				main.add(row2);
			}

			if(q.getPossibleAns().length >2){
				buttonGroup.add(radioButtons[2]);
				row3.add(radioButtons[2]);
				main.add(row3);

			}

			if(q.getPossibleAns().length >3){
				buttonGroup.add(radioButtons[3]);
				row4.add(radioButtons[3]);
				main.add(row4);
			}
			
			if(q.getPossibleAns().length >4){
				buttonGroup.add(radioButtons[4]);
				row5.add(radioButtons[4]);
				main.add(row5);
			}
		}

		// question is not of multiple choice type
		else{ System.out.println("Made open ended question");
			oeResponse = new JTextArea("Type Response Here",50,50);
			oeResponse.addFocusListener(this);
			scroll = new JScrollPane(oeResponse);
			scroll.setPreferredSize(new Dimension(250, 50));
			scroll.getVerticalScrollBar();
			scroll.getHorizontalScrollBar();
			row1.add(scroll);
			main.add(row1);
		}

		this.add(left, BorderLayout.WEST);
		this.add(main, BorderLayout.CENTER);
		this.add(right, BorderLayout.EAST);
		this.setSize(1000, 500);
		this.setVisible(true);
		//return questionPanel;
	}
	
	/**
	 * method to disable jButtons
	 */
	public void disableJButtons(){
		for(int i = 0; i<radioButtons.length; i++){
			radioButtons[i].setEnabled(false);
		}
	}
	
	/**
	 * method to disable oeResponse field
	 */
	public void disableTextArea(){
		oeResponse.setEditable(false);
	}
	
	/**
	 * method to enable jButtons
	 */
	public void enableJButtons(){
		for(int i = 0; i<radioButtons.length; i++){
			radioButtons[i].setEnabled(true);
		}
	}
	
	/**
	 * method to enable oeResponse field
	 */
	public void enableTextArea(){
		oeResponse.setEditable(true);
	}
	
	
	
	/**
	 * methods required to implement focus listener interface
	 */
	public void focusGained(FocusEvent e){
		if(e.getSource() == oeResponse){
			oeResponse.selectAll();
		}
	}
	
	public void focusLost(FocusEvent e){}

	/**
	 * method required to implement action listener interface
	 */
	public void actionPerformed(ActionEvent e){
		SystemController sc = GUIMaster.getWorkhorse();
		if(e.getSource() == removeButton){
			System.out.println("remove works");
			sc.deleteQuestion(qid);
			this.setVisible(false);
		}
	}
	
	/**
	 * method to get the users response to this question
	 * @return users response
	 */
	public Response getUserResponse(){
		if(type == 2)
			return usersResponse;
		else{
			if(oeResponse.getText().equals("Type Response Here"))
				return null;
			else
				return new Response(oeResponse.getText(), qid);
		}
			
	}
	
	private class RadioButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e){

			if(e.getSource() == radioButtons[0] && possibleAns.length >0){
				System.out.println("button0 works");
				usersResponse = new Response(radioButtons[0].getText(), qid);
			}

			else if(e.getSource() == radioButtons[1] && possibleAns.length >1){
				System.out.println("button1 works");
				usersResponse = new Response(radioButtons[1].getText(), qid);
			}

			else if(e.getSource() == radioButtons[2] && possibleAns.length >2){
				System.out.println("button2 works");
				usersResponse = new Response(radioButtons[2].getText(), qid);
			}

			else if(e.getSource() == radioButtons[3] && possibleAns.length >3){
				System.out.println("button3 works");
				usersResponse = new Response(radioButtons[3].getText(), qid);
			}

			else if(e.getSource() == radioButtons[4] && possibleAns.length >4){
				System.out.println("button4 works");
				usersResponse = new Response(radioButtons[4].getText(), qid);
			}

		}
	}
	

	
	public static void main(String[] args){
		JFrame f = new JFrame();
		JPanel main = new JPanel(new BorderLayout());
		JScrollPane scroll = new JScrollPane();
		JPanel jp = new JPanel(new GridLayout(4,1));
		
		scroll = new JScrollPane(main);
		scroll.getVerticalScrollBar();
		scroll.getHorizontalScrollBar();
		String[] ar = {"ok", "good", "fair", "average", "bad"};
		Question q = new Question("How are you?", 1, ar, 0);
		Question q2 = new Question("How can this course be improved?", 2, 1);
		
		QuestionPanel qp1 = new QuestionPanel(q,1,1);
		QuestionPanel qp2 = new QuestionPanel(q2, 2,1);
		
		qp1.disableJButtons();

		jp.add(qp1);
		jp.add(qp2);

		main.add(jp, BorderLayout.CENTER);
		f.add(scroll);
		f.setSize(1000, 500);
		f.setVisible(true);
	}
}